package com.yanqu.road.dao.impl.activity.soulbone;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.activity.soulbone.config.SoulBoneLotteryInfo;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class SoulBoneLotteryConfigDaoImpl extends TempDao {

    private static String getSoulBoneLotteryInfoSql = "select * from `t_s_activity_soulbone_lottery` where `activityId`=? ORDER BY `id` ASC";
    public List<SoulBoneLotteryInfo> getSoulBoneLotteryInfoList(int activityId) {
        List<SoulBoneLotteryInfo> list = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getSoulBoneLotteryInfoSql);
            call.setInt(1, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                SoulBoneLotteryInfo soulBoneLotteryInfo = new SoulBoneLotteryInfo();
                soulBoneLotteryInfo.setActivityId(rs.getInt("activityId"));
                soulBoneLotteryInfo.setId(rs.getInt("id"));
                soulBoneLotteryInfo.setReward(rs.getString("reward"));
                soulBoneLotteryInfo.setWeight(rs.getInt("weight"));
                soulBoneLotteryInfo.setQuality(rs.getInt("quality"));
                soulBoneLotteryInfo.setName(rs.getString("name"));

                List<String> rewardList = new ArrayList<>();
                List<Integer> weightList = new ArrayList<>();
                List<String> stringList = StringUtils.stringToStringList(soulBoneLotteryInfo.getReward(), "#");
                for (String items : stringList) {
                    List<String> rewardWeightList = StringUtils.stringToStringList(items, ",");
                    if(rewardWeightList.size() == 2) {
                        rewardList.add(rewardWeightList.get(0));
                        weightList.add(Integer.parseInt(rewardWeightList.get(1)));
                    }
                }
                soulBoneLotteryInfo.setRewardList(rewardList);
                soulBoneLotteryInfo.setWeightList(weightList);

                list.add(soulBoneLotteryInfo);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }
}
